package com.appsmith.server.domains;

import com.appsmith.external.models.BaseDomain;
import com.appsmith.external.views.Views;
import com.appsmith.server.constants.Url;
import com.fasterxml.jackson.annotation.JsonView;
import jakarta.validation.constraints.NotBlank;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Set;

@Getter
@Setter
@ToString
@NoArgsConstructor
@Document
@FieldNameConstants
public class Workspace extends BaseDomain {

    @JsonView(Views.Public.class)
    private String domain;

    @NotBlank(message = "Name is mandatory")
    @JsonView(Views.Public.class)
    private String name;

    @JsonView(Views.Public.class)
    private String website;

    @JsonView(Views.Public.class)
    private String email;

    @JsonView(Views.Public.class)
    private Set<WorkspacePlugin> plugins;

    @JsonView(Views.Public.class)
    private String slug;

    @JsonView(Views.Public.class)
    private Boolean isAutoGeneratedWorkspace;

    @JsonView(Views.Internal.class)
    private String logoAssetId;

    @Deprecated
    // TODO: Remove this field once we have migrated the data to use organizationId instead of tenantId
    @JsonView(Views.Public.class)
    private String tenantId;

    @JsonView(Views.Public.class)
    private String organizationId;

    @JsonView(Views.Internal.class)
    private Boolean hasEnvironments;

    @JsonView(Views.Internal.class)
    private Set<String> defaultPermissionGroups;

    public String makeSlug() {
        return toSlug(name);
    }

    public static String toSlug(String text) {
        return text == null ? null : text.replaceAll("[^\\w\\d]+", "-").toLowerCase();
    }

    @JsonView(Views.Public.class)
    public String getLogoUrl() {
        return Url.ASSET_URL + "/" + logoAssetId;
    }

    public static class Fields extends BaseDomain.Fields {}
}
